<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseItemCategoryTypeBase extends Base {
	var $table = '';
	var $prefix = '';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $format = true;
	var $format_check = false;
	var $format_prefix = 'field_';
	var $item_link = false;
	var $item_link_owner = false;
	var $item_fields = array('id', 'name', 'link');
	var $list_fields = array();
	var $search_fields = array('name');
	var $search_type = 'or';
	var $sort_order = true;
	var $sort_fields = array('sort_order+', 'name+');
	var $filter_fields = array();
	var $filter_map = array();
	var $filter_query = array();
	
	//
	var $table_to_category = '';
	var $table_category = '';
	
	//
	var $type = '';
	var $types = array();
	var $type_limit = true;
	
	//constructor
	function BaseItemCategoryTypeBase() {
		parent::Base();
	}
	
	function setProperty($name, $value = '') {
		if( is_array($name) ) {
			foreach($name as $k => $v) {
				$this->$k = $v;
			}
		} else {
			$this->$name = $value;
		}
	}
	
	function getProperty($name) {
		if( isset($this->$name) ) {
			return $this->$name;
		}
		
		return NULL;
	}
	
	function property($data) {
		if( is_array($data) ) {
			return $this->setProperty($data);
		} else {
			return $this->getProperty($data);
		}
	}
	
	function _pk($check = false) {
		if( $check == true && $this->format == true ) {
			return $this->pkey;
		}
		
		return $this->prefix . $this->pkey;
	}
	
	function _pf($field, $check = false) {
		if( $check == true && $this->format == true ) {
			return $field;
		}
		
		return $this->prefix . $field;
	}
	
	function setFilterQuery($name, $value = '') {
		if( is_array($name) ) {
			$this->filter_query = $name;
		} else {
			$this->filter_query[$name] = $value;
		}
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		$query_str .= $this->_query_type();
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str . $query_limit_str;
		
		$rows = $this->adb->getRows($query);
		
		//format
		for($i=0, $n=sizeof($rows); $i<$n; $i++) {
			$rows[$i] = $this->_format_handler($rows[$i]);
		}
		
		return $rows;
	}
	
	function getItemsTotal($filter = '', $check = 0, $limit = true) {
		$query_str = "";
		$query_str .= $this->_get_query_str($filter, $check);
		$query_str .= $this->_query_type();
		$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
		
		return $this->adb->getCount($query);
	}
	
	function getLatestItems($limit_length = 5) {
		if( $limit_length > 0 ) {
			
		} else {
			$limit_lenght = 5;
		}
		$query_limit_str = " limit " . $limit_length;
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. " order by " . $this->prefix . "date_added desc " . $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getLikeItems($value, $type = 'keyword') {
		$filter = array();
		if( $type == 'prefix' ) {
			$filter['prefix'] = $value . ':';
		} elseif( $type == 'keyword' ) {
			$filter['keyword'] = $value;
		} elseif( $type == 'type' ) {
			$filter['type'] = $value;
		}
		
		return $this->getItems($filter);
	}
	
	//
	function addItem($data_array) {
		$data_array = func_db_input($data_array);
		
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			$data_array[$p . $this->_table_owner_field] = $this->owner;
		}
		$data_array[$this->prefix . 'type'] = $this->_get_type();
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		$items_id = $this->_insert($this->table, $data_array);
		
		//
		if( $this->table_to_category != '' ) {
			$query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) " 
					. " values ('" . $items_id . "', '" . (int)$data_array['categories_id'] . "')";
			$this->adb->query($query);
		}
		
		//link
		if( $items_id > 0 && $this->item_link == true ) {
			$name = isset($data_array[$this->prefix . 'subject']) ? $data_array[$this->prefix . 'subject'] : '';
			if( $name == '' ) {
				$name = isset($data_array[$this->prefix . 'name']) ? $data_array[$this->prefix . 'name'] : '';
			}
			$this->_generate_item_link($items_id, $name, $data_array[$this->prefix . 'link']);
		}
		
		return $items_id;
	}
	
	function addItemB($link, $data_array) {
		$data_array[$this->prefix . 'link'] = $link;
		
		return $this->addItem($data_array);
	}
	
	function addItemCategory($items_id, $categories_id) {
		$query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' " 
				. " where " . $this->prefix . "id = '" . (int)$items_id . "'";
		
		return $this->adb->update($query);
	}
	
	function addItemToCategory($items_id, $categories_id) {
		if( $items_id > 0 && $categories_id > 0 ) {
			$query = "select count(*) as count from " . $this->table_to_category . " " 
					. " where " . $this->prefix . "id = '" . (int)$items_id . "' and categories_id = '" . (int)$categories_id . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) " 
						. " values ('" . (int)$items_id . "', '" . (int)$categories_id . "')";
				$result = $this->adb->query($query);
				
				return true;
			} elseif( $nums == 1 ) {
				return 1;
			} elseif( $nums > 1 ) {
				return 2;
			}
		}
		
		return false;
	}
	
	function addItemView($items_id) {
		$query = "update " . $this->table . " set " . $this->prefix . "viewed = " . $this->prefix . "viewed + 1 " 
				. " where " . $this->prefix . "id = '" . (int)$items_id . "'";
		
		return $this->adb->query($query);
	}
	
	function addItems($data_array) {
		$items_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$items_ids[] = $this->addItem($v);
			}
		}
		
		return $items_ids;
	}
	
	function checkItem($value, $type = 'id', $check = 0) {
		$row = $this->getItem($value, $type, $check);
		if( is_array($row) ) {
			return true;
		}
		
		return false;
	}
	
	function getItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str = " and " . $this->prefix . "status = '1'";
		}
		$query_str .= $this->_query_type();
		if( $type == 'id' ) {
			$query_str .= " and " . $this->prefix . "id = '" . (int)$value . "' ";
		} elseif( $type == 'link' ) {
			$query_str .= " and " . $this->prefix . "link = '" . $value . "' ";
		} else {
			return false;
		}
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
		$row = $this->adb->getRow($query);
		
		//format
		$row = $this->_format_handler($row);
		
		return $row;
	}
	
	function getPreviousItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str = " and " . $this->prefix . "status = '1'";
		}
		$query_str .= $this->_query_type();
		$query_str .= " order by " . $this->prefix . "id desc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item[$this->prefix . 'id'];
		}
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and " . $this->prefix . "id < '" . (int)$value . "' " 
				. $query_str . $query_limit_str;
		
		$row = $this->adb->getRow($query);
		
		//format
		$row = $this->_format_handler($row);
		
		return $row;
	}
	
	function getNextItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str = " and " . $this->prefix . "status = '1'";
		}
		$query_str .= $this->_query_type();
		$query_str .= " order by " . $this->prefix . "id asc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item[$this->prefix . 'id'];
		}
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and " . $this->prefix . "id > '" . (int)$value . "' " 
				. $query_str . $query_limit_str;
		
		$row = $this->adb->getRow($query);
		
		//format
		$row = $this->_format_handler($row);
		
		return $row;
	}
	
	function getCategoryID($items_id) {
		$query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		$row = $this->adb->getRow($query);
		
		return $row['categories_id'];
	}
	
	function getCategory($items_id) {
		$query = "select c.* from " . $this->table_category . " c, " . $this->table . " a, " . $this->table_to_category . " a2c " 
				. " where c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = a." . $this->prefix . "id " 
				. " and a." . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getCategories($items_id) {
		$query = "select c.* from " . $this->table_category . " c, " . $this->table_to_category . " a2c " 
				. " where c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = '" . (int)$items_id . "'";
		
		return $this->adb->getRows($query);
	}
	
	function getCategoriesID($items_id) {
		$query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row['categories_id'];
		}
		
		return $rows;
	}
	
	function updateItem($items_id, $data_array) {
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, "" . $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		return $this->_update($this->table, $data_array, "" . $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function updateItemD($value, $data_array, $type = 'link') {
		$value = func_db_input($value);
		$data_array = func_db_input($data_array);
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		if( $type == 'link' ) {
			$parameters = $this->prefix . "link = '" . $value . "'";
		} elseif( $type == 'id' ) {
			$parameters = $this->prefix . "id = '" . (int)$value . "'";
		}
				
		return $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $parameters);
	}
	
	function updateItemB($value, $data_array, $type = 'link') {
		$page = $this->getItem($value, $type, -1);
		if( $page[$this->prefix . 'id'] > 0 ) {
			$this->updateItem($page[$this->prefix . 'id'], $data_array);
		} else {
			$this->addItemB($value, $data_array);
		}
	}
	
	function updateItemCategory($items_id, $categories_id) {
		$query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' " 
				. " where " . $this->prefix . "id = '" . (int)$items_id . "'";
		
		return $this->adb->update($query);
	}
	
	function updateLink($items_id, $items_link) {
		$items_link = func_db_input($items_link);
		
		$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. " and " . $this->prefix . "id != '" . (int)$items_id . "' and " . $this->prefix . "link = '" . $items_link . "'";
		$row = $this->adb->getRow($query);
		if( $row['count'] > 0 ) {
			return -1;
		} else {
			$query = "update " . $this->table . " set " . $this->prefix . "link = '" . $items_link . "' where " . $this->_query_owner_rt() . " " 
					. " and " . $this->prefix . "id = '" . (int)$items_id . "'";
			$result = $this->adb->query($query);
		}
		
		return 1;
	}
	
	function updateItems($items_ids, $data_array) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->_update($this->table, $data_array, $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->_update($this->table, $data_array, $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")");
			}
		}
	}
	
	function updateItemsF($items_ids, $field, $value = '') {
		if( $field == 'status_open' ) {
			$query_str = " " . $this->prefix . "status = '1' ";
		} elseif( $field == 'status_close' ) {
			$query_str = " " . $this->prefix . "status = '0' ";
		} elseif( $field == 'public' ) {
			$query_str = " " . $this->prefix . "status = '1' ";
		} elseif( $field == 'unpublic' ) {
			$query_str = " " . $this->prefix . "status = '0' ";
		} elseif( $field == 'sort_order' ) {
			$query_str = " " . $this->prefix . "sort_order = '" . (int)$value . "' ";
		} else {
			return;
		}
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$query = "update " . $this->table . " set $query_str where " . $this->_query_owner_rt() . " " 
							. " and " . $this->prefix . "id = '" . (int)$items_id . "'";
					$this->adb->update($query);
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$query = "update " . $this->table . " set $query_str where " . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")";
				$this->adb->update($query);
			}
		}
	}
	
	function updateItemsCategory($items_ids, $categories_id) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' " 
							. " where " . $this->prefix . "id = '" . (int)$items_id . "'";
					$this->adb->update($query);
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' " 
						. " where " . $this->prefix . "id in (" . $query_ids . ")";
				return $this->adb->update($query);
			}
		}
	}
	
	function updateCategoryItemstoNewCategory($categories_id, $categories_id_new) {
		$query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id_new . "' " 
				. " where categories_id = '" . (int)$categories_id . "'";
		
		return $this->adb->update($query);
	}
	
	function deleteItem($items_id) {
		$query = "delete " . $this->table . ", " . $this->table_to_category . " " 
				. " from " . $this->table_category . " c, " . $this->table . " a, " . $this->table_to_category . " a2c " 
				. " where " . $this->_query_owner_rt('a') . " " 
				. " and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = a." . $this->prefix . "id " 
				. " and a." . $this->prefix . "id = " . (int)$items_id . " limit 1";
		
		return $this->adb->query($query);
		//
		$query = "delete from " . $this->table . " where " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'";
		$result = $this->adb->query($query);
		if( $result == false ) {
			return false;
		}
		//
		$query = "delete from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
		return $this->adb->query($query);
	}
	
	function deleteItems($items_ids) {
		$items_ids = $this->_implode(', ', $items_ids);
		$query = "delete " . $this->table . ", " . $this->table_to_category . " " 
				. " from " . $this->table_category . " c, " . $this->table . " a, " . $this->table_to_category . " a2c " 
				. " where " . $this->_query_owner_rt('a') . " " 
				. " and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = a." . $this->prefix . "id " 
				. " and a." . $this->prefix . "id in (" . $items_ids . ") limit 1";
		
		return $this->adb->query($query);
		//
		if( is_array($items_ids) ) {
			$this->_delete_ins($this->table, "" . $this->prefix . 'id', $items_ids, $this->_query_owner_rt());
		} else {
			$this->deleteItem($items_ids);
		}
	}
	
	function deleteCategoryItems($categories_id) {
		if( $this->table_to_category == '' ) {
			return false;
		}
		
		$query = "delete " . $this->table . ", " . $this->table_to_category . " " 
				. " from " . $this->table . " a, " . $this->table_to_category . " a2c " 
				. " where " . $this->_query_owner_rt('a') . " " 
				. " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id = '" . (int)$categories_id . "'";
		
		$this->adb->delete($query);
	}
	
	function setItemsStatus($items_ids, $status = 1) {
		if( is_array($items_ids) ) {
			$this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", "" . $this->prefix . 'id', $items_ids, $this->_query_owner_rt());
		}
	}
	
	//private
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and " . $this->prefix . "status > 0";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and " . $this->prefix . "name like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['category']) ) {
				if( is_array($filter['category']) ) {
					$query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and " . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "id = '" . $filter['ids'] . "' ";
				}
			}
			if( isset($filter['prefix']) && $this->_not_null($filter['prefix']) == true ) {
				$length = strlen($filter['prefix']);
				$query_str .= " and left(" . $this->prefix . "link, " . $length . ") = '" . $filter['prefix'] . "' ";
			}
			if( isset($filter['type']) && $this->_not_null($filter['type']) == true ) {
				$query_str .= " and " . $this->prefix . "type = '" . $filter['type'] . "' ";
			}
			if( isset($filter['tids']) && $this->_not_null($filter['tids']) == true ) {
				if( is_array($filter['tids']) ) {
					$query_str .= " and " . $this->prefix . "target in (" . $this->_implode(', ', $filter['tids']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "target = '" . $filter['tids'] . "' ";
				}
			}
			if( isset($filter['target']) && $this->_not_null($filter['target']) == true ) {
				if( is_array($filter['target']) ) {
					$query_str .= " and " . $this->prefix . "target in (" . $this->_implode(', ', $filter['target']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "target = '" . $filter['target'] . "' ";
				}
			}
			if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
				$qt = $this->_get_query_type();
				foreach($filter as $k => $v) {
					if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
						$k = $this->filter_map[$k];
					}
					if( in_array($k, $this->filter_fields) ) {
						$t = '';
						$p = $this->prefix;
						if( strpos($k, ':') !== false ) {
							$ks = explode(':', $k);
							$k = $ks['0'];
							$t = $ks['1'];
							if( sizeof($ks) > 2 ) {
								$p = $ks['2'];
							}
						}
						if( substr($k, 0, 1) == '=' ) {
							$k = substr($k, 1);
							$p = '';
						}
						if( !in_array($t, $qt) ) {
							$t = '';
						}
						if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
							if( $v === 'now()' || $v === 'now' ) {
								$query_str .= " and " . $p . $k . " " . $t . " now() ";
							} else {
								$query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
							}
						} elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
							if( is_array($v) ) {
								$t1 = substr($t, 0, 1);
								$t2 = substr($t, 1, 1);
								if( $t1 == '[' ) {
									$t1 = '<=';
								} elseif( $t1 == '(' ) {
									$t1 = '<';
								}
								if( $t2 == '[' ) {
									$t2 = '<=';
								} elseif( $t1 == '(' ) {
									$t2 = '<';
								}
								
								$query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
								$query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
							}
						} elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
							if( $t == 'notlike' ) {
								$query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
							} else {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							}
						} else {
							if( $v === 'now()' || $v === 'now' ) {
								$query_str .= " and " . $p . $k . " < now() ";
							} elseif( is_array($v) && sizeof($v) > 0 ) {
								$query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
							} elseif( is_int($v) ) {
								$query_str .= " and " . $p . $k . " = '" . $v . "' ";
							} elseif( is_numeric($v) ) {
								$query_str .= " and " . $p . $k . " = '" . $v . "' ";
							} elseif( is_string($v) ) {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							} else {
								$query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
							}
						}
					}
				}
			}
		}
		//
		$query_str .= $this->_get_query_str_custom($filter, $check);
		
		return $query_str;
	}
	
	function _get_query_str_custom($filter = '', $check = 0) {
		return "";
	}
	
	function _get_query_order_sort($order_by = '', $order_sort = '') {
		if( $order_by == '' ) {
			$order_by = $this->query_order_by;
		}
		if( $order_sort == '' ) {
			$order_sort = $this->query_order_sort;
		}
		if( $order_by != '' && $order_sort != '' ) {
			if( $order_by == 'dateadded' ) {
				$order_by = 'date_added';
			} elseif( $order_by == 'sortorder' ) {
				$order_by = 'sort_order';
			}
			//
			$sort_query = array();
			if( !is_array($this->sort_query) && is_array($this->sort_fields) ) {
				foreach($this->sort_fields as $k => $v) {
					if( substr($v, -1, 1) == '+' || substr($v, -1, 1) == '-' ) {
						$sort_query[] = substr($v, 0, -1);
					}
				}
			} else {
				$sort_query = $this->sort_query;
			}
			if( is_array($sort_query) && in_array($order_by, $sort_query) ) {
				$sort_fields = array($order_by . ($order_sort == 'desc' ? '+' : '-'));
				$this->sort_fields = $sort_fields;
			}
		}
	}
	
	function _query_owner_rt($pf = '') {
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			if( $pf != '' ) {
				$p = $pf . '.' . $p;
			}
			
			return $this->_query_owner($p . $this->_table_owner_field);
		}
		
		return " 1 ";
	}
	
	function _get_query_type() {
		$query_type = array(
				'=', '>', '>=', '<', '<=', '!=',
				'[]', '()', '[)', '(]',
				'like', 'notlike', '%like%', '%like', 'like%',
				'regexp', 'notregexp',
					);
		return $query_type;
	}
	
	function _get_order_str() {
		return $this->query_order_str;
	}
	
	//
	function _generate_item_link($items_id, $items_name = '', $items_link = '') {
		if( $items_link == '' && $items_name != '' ) {
			$items_link = $this->_filter_link($items_name);
		}
		if( $this->_not_null($items_link) ) {
			$items_link = func_db_input($items_link);
			$query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' " 
					. " and " . $this->prefix . "link = '" . $items_link . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
				return true;
			}
		}
		$items_link = $this->_filter_link($items_name);
		$items_link = $this->_rand_link($items_id, $items_link);
		
		$this->_generate_item_link($items_id, $items_name, $items_link);
		
		return true;
	}
	
	function _generate_item_link_detail($items_id, $items_name = '', $items_link = '') {
		if( $items_link == '' && $items_name != '' ) {
			$items_link = $this->_filter_link($items_name);
		}
		if( $this->_not_null($items_link) ) {
			$query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' " 
					. " and " . $this->prefix . "link = '" . $items_link . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
				return true;
			}
		}
		$items_link = $this->_filter_link($items_name);
		$items_link = $this->_rand_link($items_id, $items_link);
		
		$this->_generate_item_link_detail($items_id, $items_name, $items_link);
		
		return true;
	}
	
	//type
	function _query_type($p = '', $type = '') {
		$types = $this->types;
		$type = ($type == '') ? $this->type : '';
		if( in_array($type, $types) ) {
			return " and " . (($p == '') ? '' : $p . ".") . $this->prefix . "type = '" . $type . "' ";
		}
		
		return '';
	}
	
	function _get_type() {
		$types = $this->types;
		if( in_array($this->type, $types) ) {
			return $this->type;
		}
		
		return '';
	}
	
	//target
	function deleteTargetItems($target_id) {
		$query = "delete from " . $this->table . " where " . $this->prefix . "target = '" . (int)$target_id . "'";
		
		return $this->adb->query($query);
	}
	
	function deleteTargetsItems($target_ids) {
		if( is_array($items_ids) ) {
			$this->_delete_ins($this->table, "" . $this->prefix . "target", $target_ids);
		}
	}
	
	function getTargetItem($target_id, $items_id = '') {
		$query = "select * from " . $this->table . " where " . $this->prefix . "target = '" . (int)$target_id . "'";
		if( $items_id != '' ) {
			$query .= " and " . $this->prefix . "id = '" . (int)$items_id . "'";
		}
		
		return $this->adb->getRow($query);
	}
	
	function getTargetItems($target_id, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' ) {
			$filter = array();
			$filter['target'] = $target_id;
		} elseif( is_array($filter) ) {
			$filter['target'] = $target_id;
		}
		
		return $this->getItems($filter, $check, $limit);
	}
	
	function getTargetFirst($items_id) {
		$query = "select * from " . $this->table . " where " . $this->prefix . "target = '" . (int)$items_id . "' " 
				. " order by " . $this->prefix . "id asc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetLast($items_id) {
		$query = "select * from " . $this->table . " where " . $this->prefix . "target = '" . (int)$items_id . "' " 
				. " order by " . $this->prefix . "id desc limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getTargetStats($items_id) {
		$count = $this->getItemsTotal(array('pid' => $items_id));
		$first = $this->getTargetFirst($items_id);
		$last = $this->getTargetLast($items_id);
		$stats = array('count' => $count, 'first' => $first, 'last' => $last);
		
		return $stats;
	}
	
	//private
	function _format_handler($item) {
		if( isset($item[$this->prefix . 'name']) ) {
			$item[$this->prefix . 'name'] = stripslashes($item[$this->prefix . 'name']);
		}
		if( isset($item[$this->prefix . 'subject']) ) {
			$item[$this->prefix . 'subject'] = stripslashes($item[$this->prefix . 'subject']);
		}
				
		return $item;
	}
}
//
?>